package com.indobrains.uud1945;

import android.content.Context;
import android.database.Cursor;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.SimpleCursorTreeAdapter;
import android.widget.TextView;

import com.indobrains.uud1945.database.DatabaseAdapter;

public class Pasal2Adapter extends SimpleCursorTreeAdapter {

	private DatabaseAdapter db;
	private int layoutGroup;
	private int layoutChild;
	private Context mContext;
	@Override
	public View newChildView(Context context, Cursor cursor,
			boolean isLastChild, ViewGroup parent) {
		// TODO Auto-generated method stub
		//return super.newChildView(context, cursor, isLastChild, parent);
		final LayoutInflater inflater = LayoutInflater.from(context);
		View v = inflater.inflate(layoutChild, parent, false);
		return v;
	}

	@Override
	public View newGroupView(Context context, Cursor cursor,
			boolean isExpanded, ViewGroup parent) {
		// TODO Auto-generated method stub
		//return super.newGroupView(context, cursor, isExpanded, parent);
		final LayoutInflater inflater = LayoutInflater.from(context);
		View v = inflater.inflate(layoutGroup, parent, false);
		return v;
	}

	public Pasal2Adapter(Context context, Cursor cursor, int groupLayout,
			String[] groupFrom, int[] groupTo, int childLayout,
			String[] childFrom, int[] childTo) {
		super(context, cursor, groupLayout, groupFrom, groupTo, childLayout, childFrom,
				childTo);
		// TODO Auto-generated constructor stub
		 
		this.layoutChild=childLayout;
		this.layoutGroup=groupLayout;
		this.mContext=context;
		db=new DatabaseAdapter(mContext);
		db.Open();
	}

	@Override
	protected Cursor getChildrenCursor(Cursor cg) {
		// TODO Auto-generated method stub
		int ClauseID=cg.getInt(cg.getColumnIndex(db.CLAUSE_ID));
		Cursor cc=db.getContents(ClauseID);
		return cc;
	}

	@Override
	protected void bindChildView(View view, Context context, Cursor cursor,
			boolean isLastChild) {
		// TODO Auto-generated method stub
		//super.bindChildView(view, context, cursor, isLastChild);
		
		String s_bullet=cursor.getString(cursor.getColumnIndex(db.CONTENT_BULLET));
		String s_content=cursor.getString(cursor.getColumnIndex(db.CONTENT_CONTENT));
		String s_older=cursor.getString(cursor.getColumnIndex(db.CONTENT_OLDER));
		String s_amandemen=cursor.getString(cursor.getColumnIndex(db.AMANDEMEN_DESC));
		
		TextView bullet=(TextView)view.findViewById(R.id.bullet);
		TextView content=(TextView)view.findViewById(R.id.isi_pasal);
		TextView amandemen=(TextView)view.findViewById(R.id.amandemen);
		TextView older=(TextView)view.findViewById(R.id.older_version);
		bullet.setText(s_bullet);
		content.setText(s_content);
		amandemen.setText(s_amandemen);
		older.setText(s_older);
	}




	@Override
	protected void bindGroupView(View view, Context context, Cursor cursor,
			boolean isExpanded) {
		// TODO Auto-generated method stub
		//super.bindGroupView(view, context, cursor, isExpanded);
		int clause_id=cursor.getInt(cursor.getColumnIndex(db.CLAUSE_ID));
		int ccount=db.getParagraphCount(clause_id);
		String title=cursor.getString(cursor.getColumnIndex(db.CLAUSE_TITLE));
		TextView judul=(TextView)view.findViewById(R.id.judulPasal);
		TextView jumlah=(TextView)view.findViewById(R.id.clause_count);
		judul.setText(title);
		jumlah.setText(" ada "+Integer.toString(ccount)+" Ayat");
	}

}
